package org.blog.run;

import java.util.concurrent.CountDownLatch;

public class MultiThread {

	/**
	 * 
	 * @param object
	 *            传入对象
	 * @param count
	 *            线程数目
	 */
	public void execute(Object object, int count) throws Exception {
		CountDownLatch startLatch = new CountDownLatch(1);
		CountDownLatch endLatch = new CountDownLatch(count);
		for (int i = 0; i < count; i++) {
			Service myService=new MyService(object);
			new Thread(new MyRunner(startLatch, endLatch,myService )).start();
		}

		startLatch.countDown();

		long startTime = System.currentTimeMillis();
		System.out.println(startTime);
		endLatch.await();
		long endTime = System.currentTimeMillis();
		System.out.println("the total of the runnable time is "
				+ (endTime - startTime) / 1000 + " seconds");
	}

	public static void main(String[] args) throws Exception {
		MultiThread multiThread = new MultiThread();
		// 此处对象传入你要传入的参数，例如你要分解读取的map
		multiThread.execute(null, 3);
	}

}

class MyService extends Service {

	public MyService(Object o) {
		super(o);
	}

	@Override
	public void execute() {
		// TODO 自己处理的业务
	}

}

class MyRunner implements Runnable {

	private CountDownLatch start;
	private CountDownLatch end;
	private Service service;

	public MyRunner(CountDownLatch start, CountDownLatch end, Service service) {
		this.start = start;
		this.end = end;
		this.service = service;
	}

	@Override
	public void run() {
		try {
			start.await();
			service.execute();
			end.countDown();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
